package org.jeecg.modules.dongchen.service.impl;

import org.jeecg.modules.dongchen.entity.DcHome;
import org.jeecg.modules.dongchen.entity.DcLunbo;
import org.jeecg.modules.dongchen.entity.DcAnli;
import org.jeecg.modules.dongchen.mapper.DcLunboMapper;
import org.jeecg.modules.dongchen.mapper.DcAnliMapper;
import org.jeecg.modules.dongchen.mapper.DcHomeMapper;
import org.jeecg.modules.dongchen.service.IDcHomeService;
import org.jeecg.modules.dongchen.vo.DcHomePage;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 首页
 * @Author: jeecg-boot
 * @Date:   2022-03-01
 * @Version: V1.0
 */
@Service
public class DcHomeServiceImpl extends ServiceImpl<DcHomeMapper, DcHome> implements IDcHomeService {

	@Autowired
	private DcHomeMapper dcHomeMapper;
	@Autowired
	private DcLunboMapper dcLunboMapper;
	@Autowired
	private DcAnliMapper dcAnliMapper;

	@Override
	@Transactional
	public void saveMain(DcHome dcHome, List<DcLunbo> dcLunboList,List<DcAnli> dcAnliList) {
		dcHomeMapper.insert(dcHome);
		if(dcLunboList!=null && dcLunboList.size()>0) {
			for(DcLunbo entity:dcLunboList) {
				//外键设置
				entity.setPid(dcHome.getId());
				dcLunboMapper.insert(entity);
			}
		}
		if(dcAnliList!=null && dcAnliList.size()>0) {
			for(DcAnli entity:dcAnliList) {
				//外键设置
				entity.setPid(dcHome.getId());
				dcAnliMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(DcHome dcHome,List<DcLunbo> dcLunboList,List<DcAnli> dcAnliList) {
		dcHomeMapper.updateById(dcHome);

		//1.先删除子表数据
		dcLunboMapper.deleteByMainId(dcHome.getId());
		dcAnliMapper.deleteByMainId(dcHome.getId());

		//2.子表数据重新插入
		if(dcLunboList!=null && dcLunboList.size()>0) {
			for(DcLunbo entity:dcLunboList) {
				//外键设置
				entity.setPid(dcHome.getId());
				dcLunboMapper.insert(entity);
			}
		}
		if(dcAnliList!=null && dcAnliList.size()>0) {
			for(DcAnli entity:dcAnliList) {
				//外键设置
				entity.setPid(dcHome.getId());
				dcAnliMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		dcLunboMapper.deleteByMainId(id);
		dcAnliMapper.deleteByMainId(id);
		dcHomeMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			dcLunboMapper.deleteByMainId(id.toString());
			dcAnliMapper.deleteByMainId(id.toString());
			dcHomeMapper.deleteById(id);
		}
	}

	@Override
	public DcHomePage onePage() {
		return dcHomeMapper.selectOneHome();
	}

}
